﻿using System.Windows.Input;

namespace Xioa_UI.Service;

public class XioaRelayCommand : ICommand
{
    private Action? _execute; 

    private Func<object, bool>? _canExecute; 
    
    public event EventHandler? CanExecuteChanged; 
    
    public XioaRelayCommand(Action? execute, Func<object, bool>? canExecute = null)
    {
        this._execute = execute;

        this._canExecute = canExecute;
    }
    
    public bool CanExecute(object? parameter)
    {
        if (_canExecute != null)
            return parameter != null && _canExecute(parameter);
        
        return true; 
    }
    
    public void Execute(object? parameter)
    {
       _execute?.Invoke(); // 执行命令
    }
    
    public void RaiseCanExecuteChanged()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty); // 触发可执行性改变事件
    }
}